var adduser={
    init:function(){
        this.loadData();
        this.loadEvent();
        layui.upload.render({
            elem: '#upload',
            url: URL.INDEX.UPLOADFILE,
            done: function(res){
                if(res.state == 1){
                    var data = res.data[0];
                    $('#upload img').attr('src',data.img_host+""+data.file_name);
                    $('#headimg').val(data.id);
                }else{
                    njzxw.alert({
                        error:true,
                        msg:'上传失败'
                    })
                }
            }
        });
    },
    initSelectableOrg:null,
    initSelectableRole:null,
    orgMove:'',
    roleMove:'',
    loadEvent:function(){
        $('#orgMenumenutitle').keyup(function(){
            var val = this.value;
            adduser.initSelectableOrg.treeview('search', [ val, { attribute:'text',ignoreCase: false, exactMatch: false } ]);
        });
        $('#roleMenumenutitle').keyup(function(){
            var val = this.value;
            adduser.initSelectableRole.treeview('search', [ val, { attribute:'text',ignoreCase: false, exactMatch: false } ])
        });
    },
    /**
     * 保存操作
     * @param obj
     * @param closefn
     * @returns {Boolean}
     */
    save:function(){
        //选择组织
        var orgNode = adduser.initSelectableOrg.treeview('getChecked');
        if(orgNode.length == 0){
            njzxw.alert({
                error:true,
                msg:'请选择组织'
            })
            return false;
        }
        var orgn = ",";
        for(var i=0;i<orgNode.length;i++){
            orgn += orgNode[i].id+",";
        }
        $('#orgStr').val(orgn);

        //获取角色
        var roleNode = adduser.initSelectableRole.treeview('getChecked');
        if(roleNode.length == 0){
            njzxw.alert({
                error:true,
                msg:'请选择角色'
            })
            return false;
        }
        var role = ",";
        for(var i=0;i<roleNode.length;i++){
            role += roleNode[i].id+",";
        }
        $('#roleStr').val(role);

        return njzxw.formData('#form1');
    },
    /**
     * 加载角色跟组织信息
     */
    loadData:function(){
        //加载菜单
        njzxw.ajax({
            url:URL.USER.LOADROLEORG,
            data:{
                id:njzxw.getQueryParamsString('id')
            },
            fun:function(a,b){
                if(a){
                    if(b.state == 1){
                        console.log("====》",b);
                        var data = b.org;
                        var cnt = data.length;
                        var href = "#";
                        var json = [];
                        for(var i=0;i<cnt;i++){
                            var id0 = data[i].id;
                            var org_name0 = data[i].org_name;
                            var supper_id0 = data[i].supper_id;
                            var levels0 = data[i].levels;
                            var da0 = {};
                            if(supper_id0 == 0){//第一级
                                da0.text = org_name0;
                                da0.id = id0;
                                da0.supper_id = supper_id0;
                                var nodes0 = adduser.loadNextOrg(data,id0);

                                if(nodes0.length != 0){
                                    da0.nodes = nodes0;
                                }else{
                                    da0.href = '#'+id0;
                                }
                                da0.tags = [''+nodes0.length+''];
                                json.push(da0);
                            }
                        }
                        adduser.initSelectableOrg = $('#orgMenu').treeview({
                            color: "#428bca",
                            expandIcon: 'glyphicon glyphicon-chevron-right',
                            collapseIcon: 'glyphicon glyphicon-chevron-down',
                            nodeIcon: 'glyphicon glyphicon-bookmark',
                            showIcon:false,
                            showTags: true,
                            highlightSelected:false,
                            showCheckbox:true,
                            multiSelect:true,
                            levels: 1,
                            data: json,
                            //勾选事件
                            onNodeChecked:function(event, node){
                                if(adduser.orgMove == node.id){
//									adduser.initSelectableOrg.treeview('uncheckAll');
                                    var checkableNodes1 = adduser.initSelectableOrg.treeview('getChecked');
                                    for(var i=0;i<checkableNodes1.length;i++){
                                        if(checkableNodes1[i].id != node.id){
                                            adduser.initSelectableOrg.treeview('uncheckNode', [ checkableNodes1[i], { silent: $('#chk-check-silent').is(':checked') }]);
                                        }
                                    }
                                }
                                //勾选父级菜单
//								var checkableNodes = adduser.initSelectableOrg.treeview('search', [ node.supper_id, { attribute:'id',ignoreCase: false, exactMatch: true } ]);
//								adduser.initSelectableOrg.treeview('checkNode', [ checkableNodes, { silent: $('#chk-check-silent').is(':checked') }]);
//
                                var checkableNodes1 = adduser.initSelectableOrg.treeview('search', [ node.id, { attribute:'supper_id',ignoreCase: false, exactMatch: true } ]);
                                adduser.initSelectableOrg.treeview('checkNode', [ checkableNodes1, { silent: $('#chk-check-silent').is(':checked') }]);
                                adduser.initSelectableOrg.treeview('clearSearch');
                            },
                            //取消勾选事件
                            onNodeUnchecked:function(event, node){
                                adduser.initSelectableOrg.treeview('checkNode', [ node, { silent: $('#chk-check-silent').is(':checked') }]);
                            },
                            onMoveIn:function(event, node){
                                //common.log(node);
                                adduser.orgMove = node.id;
                            },
                            onMoveOut:function(event, node){
                                //common.log(node);
                                adduser.orgMove = '';
                            }
                        });
                        //加载角色信息
                        var dataRole = b.role;
                        var cntRole = dataRole.length;
                        var hrefRole = "#";
                        var jsonRole = [];
                        for(var i=0;i<cntRole;i++){//取层级关系 第一级
                            var da = {
                                id:dataRole[i].id,
                                text:dataRole[i].role_name,
                                isdelete:dataRole[i].isdelete
                            };
                            jsonRole.push(da);
                        }
                        adduser.initSelectableRole = $('#roleMenu').treeview({
                            color: "#428bca",
                            expandIcon: 'glyphicon glyphicon-chevron-right',
                            collapseIcon: 'glyphicon glyphicon-chevron-down',
                            nodeIcon: 'glyphicon glyphicon-bookmark',
                            showIcon:false,
                            showTags: true,
                            highlightSelected:false,
                            showCheckbox:true,
                            levels: 1,
                            data: jsonRole
                        });
                        //给对应的组织与角色进行赋值
                        var orgStr = b.user.org_id.split(',');
                        for(var i=0;i<orgStr.length;i++){
                            var orgNode = adduser.initSelectableOrg.treeview('search', [ orgStr[i], { attribute:'id',ignoreCase: false, exactMatch: false } ]);
                            adduser.initSelectableOrg.treeview('checkNode', [ orgNode, { silent: $('#chk-check-silent').is(':checked') }]);
                        }
                        adduser.initSelectableOrg.treeview('clearSearch');
                        var roleStr = b.user.role_id.split(',');
                        for(var i=0;i<roleStr.length;i++){
                            var roleNode = adduser.initSelectableRole.treeview('search', [ roleStr[i], { attribute:'id',ignoreCase: false, exactMatch: false } ]);
                            adduser.initSelectableRole.treeview('checkNode', [ roleNode, { silent: $('#chk-check-silent').is(':checked') }]);
                        }
                        adduser.initSelectableRole.treeview('clearSearch');
                    }else{
                        njzxw.alert({
                            error:true,
                            msg:"角色跟组织信息加载失败"
                        });
                    }
                }else{
                    njzxw.alert({
                        error:true,
                        msg:"角色跟组织信息加载失败"
                    });
                }
            }
        });
    },
    /**
     * 重汇组织选择器
     */
    loadNextOrg:function(data,id){
        var cnt = data.length;
        var href = "#";
        var json = [];
        for(var i=0;i<cnt;i++){
            var id0 = data[i].id;
            var org_name0 = data[i].org_name;
            var supper_id0 = data[i].supper_id;
            var levels0 = data[i].levels;
            var da0 = {};
            if(supper_id0 == id){//第一级
                da0.text = org_name0;
                da0.id = id0;
                da0.supper_id = supper_id0;
                var nodes0 = adduser.loadNextOrg(data,id0);

                if(nodes0.length != 0){
                    da0.nodes = nodes0;
                }else{
                    da0.href = '#'+id0;
                }
                da0.tags = [''+nodes0.length+''];
                json.push(da0);
            }
        }
        return json;
    },
    /**
     * table表格参数处理
     * @returns {Array}
     */
    fnTableParam:function(){
        return [{name:'title',value:$('#title').val()}];
    }
};